package org.example.fancystudy.Lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author chaigq
 * @version 1.0
 * @date 2022/11/9 23:45
 */
public class ReEntryLockDemo2 {
    private static ReentrantLock reentrantLock = new ReentrantLock();
    public static void main(String[] args) {

        new Thread(() -> {
            reentrantLock.lock();
            try {
                System.out.println(Thread.currentThread().getName()+"\t ----t1外层 come in");
                reentrantLock.lock();
                try {
                    System.out.println(Thread.currentThread().getName()+"\t ----t1内层 come in");
                }finally {
                    reentrantLock.unlock();
                }
            }finally {
                //如果不能一一对应lock，会造成阻塞
                //reentrantLock.unlock();
            }
        }, "t1").start();

        new Thread(() -> {
            reentrantLock.lock();
            try {
                System.out.println(Thread.currentThread().getName()+"\t ----t2外层 come in");
            }finally {
                reentrantLock.unlock();
            }
        }, "t2").start();
    }
}
